package com.app.xinfadi.ui.purchase_plan.center_purchase_plan.new_bill_of_sales_status;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseFragment;
import com.app.xinfadi.base.MyApplication;
import com.app.xinfadi.dao.HomeDao;
import com.app.xinfadi.ui.MainActivity;
import com.app.xinfadi.ui.purchase_plan.center_purchase_plan.NewBillOfSalesBtnActivity;
import com.app.xinfadi.util.Toast;
import com.app.xinfadi.widget.gson.Success;
import com.google.gson.Gson;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * function: 出货登记单——部分完成
 * Created by Wutengfei on 2016/1/25.
 */
public class NewBillOfSalesNotFinishFragment extends BaseFragment {
    private View view;
    private String currentStatus = "05";
    private String plan_id;
    private EditText resultEdt;
    private Button submitBtn;

    public static NewBillOfSalesNotFinishFragment getInstance(String plan_id) {
        NewBillOfSalesNotFinishFragment instance = new NewBillOfSalesNotFinishFragment();
        Bundle args = new Bundle();
        args.putString("plan_id", plan_id);
        instance.setArguments(args);
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.new_bill_of_sales_not_finish_layout_fragment, null);
        plan_id = (String) getArguments().get("plan_id");
        initView();
        initData();
        return view;
    }

    private void initView() {
        resultEdt = (EditText) view.findViewById(R.id.resultEdt);
        submitBtn = (Button) view.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);
    }

    private void initData() {

    }

    private void upload() {
        HomeDao homeDao = new HomeDao();
        try {
            homeDao.finishPlans(new File[]{}, plan_id,resultEdt.getText().toString().trim(), currentStatus, "", new TextHttpResponseHandler() {
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    Toast.makeText(context, "登出记录失败！", Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    Success success = new Gson().fromJson(responseString, Success.class);
                    if(success.success){
                        Toast.makeText(context, "登出记录成功！", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(context, "登出记录失败！", Toast.LENGTH_SHORT).show();
                    }

                }
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submitBtn:
                upload();
                ((NewBillOfSalesBtnActivity)context).popAllActivityExceptOne(MainActivity.class);
                break;
        }
    }
}
